package com.greenlaw110.cimple.controller;

import act.app.AppContext;
import act.controller.Controller;
import com.greenlaw110.cimple.model.Admin;
import org.osgl.aaa.NoAuthenticate;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;

import static act.controller.Controller.Util.*;

@Controller("/admin")
@NoAuthenticate
public class AdminLoginController {
    @NoAuthenticate
    @GetAction("/login")
    public void login() {
        String title = "Cimple后台登陆";
        render(title);
    }

    @NoAuthenticate
    @PostAction("/login")
    public void loginAction(String username, String password, AppContext context) {
        boolean loggedIn = Admin.authenticate(username, password);
        if (loggedIn) {
            context.session().put("username", username);
            redirect("/admin");
        } else {
            redirect("/admin/login");
        }
    }
}
